package com.example.shirodemo.controller.admin;

import cn.hutool.core.lang.Assert;
import com.example.shirodemo.constant.HttpCode;
import com.example.shirodemo.dto.ResponseResult;
import com.example.shirodemo.dto.ResponseResultPage;
import com.example.shirodemo.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 绫小路
 * @date 2021/1/11 21:10
 * @description
 */
@Api(tags = "04-后台页面：授权管理 - PermissionController")
@Controller
@RequestMapping("/admin/permission")
public class PermissionController {

  @Autowired
  private AdminService adminService;
  @Autowired
  private HttpServletRequest request;

  @ApiOperation("权限管理")
  @GetMapping("")
  public String index() {
    return "admin/admin-permission";
  }

  @ApiOperation("后台管理获取所有权限列表")
  @GetMapping("list")
  @ResponseBody
  public ResponseResultPage list(Integer page, Integer limit, String search) {
    if ((page != null && (page < 1 || page > 100)) ||
        (limit != null && (limit < 1 || limit > 100))) {
      return new ResponseResultPage(HttpCode.FAIL, "入参page、limit范围分别是：1~100、1~100");
    }
    if (page == null) {
      page = 1;
    }
    if (limit == null) {
      limit = 10;
    }
    return adminService.getPermissionList(search, page, limit);
  }

  @ApiOperation("后台管理添加URL鉴权")
  @PostMapping("add")
  @ResponseBody
  public ResponseResult add(String url, String method, String description) {
    Assert.isFalse(url == null, "入参角色roleId不能为空！");
    Assert.notBlank(url, "url不能为空！");
    Assert.notBlank(method, "method不能为空！");
    Assert.isFalse(!"GET".equals(method) && !"POST".equals(method), "method只能填GET/POST");
    Assert.isFalse(description != null && description.length() > 100, "description不能超过100字符");
    return adminService.addPermission(url, method, description);
  }

  @ApiOperation("后台管理删除URL鉴权")
  @PostMapping("delete")
  @ResponseBody
  public ResponseResult delete(Integer id) {
    Assert.isFalse(id == null, "入参id不能为空！");
    return adminService.deletePermission(id);
  }

  @ApiOperation("后台管理更新URL鉴权")
  @PostMapping("update")
  @ResponseBody
  public ResponseResult update(Integer id, String url, String method, String description) {
    Assert.isFalse(id == null, "入参id不能为空！");
    Assert.isFalse(url == null, "入参角色roleId不能为空！");
    Assert.notBlank(url, "url不能为空！");
    Assert.notBlank(method, "method不能为空！");
    Assert.isFalse(!"GET".equals(method) && !"POST".equals(method), "method只能填GET/POST");
    Assert.isFalse(description != null && description.length() > 100, "description不能超过100字符");
    return adminService.updatePermission(id, url, method, description);
  }

  @ApiOperation("后台管理菜单授权页面")
  @GetMapping("menu")
  public String menu(Integer roleId) {
    Assert.isFalse(roleId == null, "入参角色roleId不能为空！");
    request.setAttribute("roleId", roleId);
    return "/admin/admin-permission-menu";
  }

  @ApiOperation("后台管理获取角色菜单")
  @GetMapping("roleMenu")
  @ResponseBody
  public ResponseResult roleMenu(Integer roleId) {
    Assert.isFalse(roleId == null, "入参角色roleId不能为空！");
    return adminService.roleMenu(roleId);
  }

  @ApiOperation("后台管理角色添加菜单")
  @PostMapping("roleAddMenu")
  @ResponseBody
  public ResponseResult roleAddMenu(Integer roleId, String ids) {
    Assert.isFalse(roleId == null, "入参角色roleId不能为空！");
    return adminService.roleAddMenu(roleId, ids);
  }

  @ApiOperation("后台管理角色授权页面")
  @GetMapping("role")
  public String role(Integer userId) {
    Assert.isFalse(userId == null, "用户userId不能为空！");
    request.setAttribute("userId", userId);
    return "admin/admin-permission-role";
  }

  @ApiOperation("后台管理获取角色列表")
  @GetMapping("roleList")
  @ResponseBody
  public ResponseResult roleList(Integer userId) {
    Assert.isFalse(userId == null, "用户userId不能为空！");
    return adminService.getUserRoleVo(userId);
  }

  @ApiOperation("后台管理更新用户角色")
  @PostMapping("updateUserRole")
  @ResponseBody
  public ResponseResult updateUserRole(String ids, Integer userId) {
    Assert.isFalse(userId == null, "用户userId不能为空！");
    return adminService.updateUserRole(userId, ids);
  }

}
